package com.homedo.odin.solution.controller.mng;

import com.homedo.core.common.base.dto.Meta;
import com.homedo.core.common.base.dto.ResultData;
import com.homedo.core.common.exception.BizException;
import com.homedo.core.common.util.ParamCheckUtil;
import com.homedo.odin.solution.enumcode.activity.HundredStylistInfoAuditStatus;
import com.homedo.odin.solution.model.request.mng.MngHundredStylistInfoListRequestBean;
import com.homedo.odin.solution.model.request.mng.MngHundredStylistInfoUpdateRequestBean;
import com.homedo.odin.solution.model.response.mng.MngHundredStylistInfoDetailResponseBean;
import com.homedo.odin.solution.model.response.mng.MngHundredStylistPageResponseBean;
import com.homedo.odin.solution.service.HundredStylistInfoServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: zhanglei
 * @date: 2018/4/25
 * @description: Mng 活动接口 API
 */
@Api(description = "活动接口", tags = {"Activity"}, produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
@RequestMapping("/mng/activity")
public class MngActivityController {

    @Autowired
    private HundredStylistInfoServiceImpl hundredStylistInfoService;


    @ApiOperation(value = "Mng_百名设计师评选列表查询", httpMethod = "GET", response = MngHundredStylistPageResponseBean.class)
    @GetMapping(value = "/hundred_stylist")
    public ResultData stylistInfoPage(@Valid @ModelAttribute MngHundredStylistInfoListRequestBean requestBean,
            BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String defaultMessage = bindingResult.getFieldError().getDefaultMessage();
            return ResultData.newInstance(Meta.CODE_451, defaultMessage);
        }

        MngHundredStylistPageResponseBean responseBean = hundredStylistInfoService.findStylistInfoPageList(requestBean);
        return ResultData.newInstance(Meta.CODE_200, responseBean);
    }


    @ApiOperation(value = "Mng_百名设计师评选详情", httpMethod = "GET", response = MngHundredStylistInfoDetailResponseBean.class)
    @GetMapping(value = "/hundred_stylist/{userId}")
    public ResultData stylistInfoDetail(@ApiParam(value = "用戶ID", required = true) @PathVariable Integer userId) {
        ParamCheckUtil.checkUserId(userId);
        MngHundredStylistInfoDetailResponseBean responseBean = hundredStylistInfoService
                .findStylistInfoByUserId(userId);
        return ResultData.newInstance(Meta.CODE_200, responseBean);
    }


    @ApiOperation(value = "Mng_百名设计师详情修改", httpMethod = "PUT")
    @PutMapping(value = "/hundred_stylist")
    public ResultData updateStylistInfoDetail(@Valid @RequestBody MngHundredStylistInfoUpdateRequestBean requestBean,
            BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String defaultMessage = bindingResult.getFieldError().getDefaultMessage();
            return ResultData.newInstance(Meta.CODE_451, defaultMessage);
        }
        hundredStylistInfoService.updateStylistInfoDetail(requestBean);
        return ResultData.newInstance(Meta.CODE_200, null);
    }


    @ApiOperation(value = "Mng_百名设计师评选审核", httpMethod = "PUT")
    @PutMapping(value = "/hundred_stylist/{auditStatus}/{userId}")
    public ResultData updateStylistInfoAudit(@ApiParam(value = "用戶ID", required = true) @PathVariable Integer userId,
            @ApiParam(value = "审核状态", required = true) @PathVariable Integer auditStatus) {
        ParamCheckUtil.checkUserId(userId);
        if (!HundredStylistInfoAuditStatus.dbValueExist(auditStatus)) {
            //提示用户审核包含的哪些参数
            Map<String, Integer> promptInfo = Arrays.stream(HundredStylistInfoAuditStatus.values()).collect(Collectors
                    .toMap(HundredStylistInfoAuditStatus::getDisplayValue, HundredStylistInfoAuditStatus::getDbValue));
            throw new BizException(Meta.CODE_451.getCode(), "auditStatus参数非法" + promptInfo);
        }

        hundredStylistInfoService.updateStylistInfoAudit(userId, auditStatus);
        return ResultData.newInstance(Meta.CODE_200, null);
    }
}
